package com.itbaizhan.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.ruiji.common.Result;
import com.itbaizhan.ruiji.pojo.Category;
import com.itbaizhan.ruiji.service.CategorySevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategorySevice categorySevice;
    @PostMapping
    public Result<String> save(@RequestBody Category category){
        log.info("保存分类信息：{}",category);
        categorySevice.save(category);
        return Result.success("添加成功");
    }
    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize){
        log.info("分页查询：{}",page,pageSize);
        Page catpage = new Page(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        categorySevice.page(catpage,queryWrapper);
        return Result.success(catpage);
    }

    @DeleteMapping
    public Result<String> remove(Long id){
        log.info("删除分类：{}",id);
        categorySevice.remove(id);
        return Result.success("删除成功");
    }
    @PutMapping
    public Result<String> update(@RequestBody Category category){
        log.info("修改分类信息：{}",category);
        categorySevice.updateById( category);
        return Result.success("修改成功");
    }


    @GetMapping("/list")
    public Result<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categorySevice.list(queryWrapper);
        return Result.success(list);
    }

}
